package com.test.myspringproject;

import java.lang.ref.WeakReference;

public class ReferenceExample {
    public static void main(String[] args) {
        Object strongObj = new Object(); // 强引用
        WeakReference<Object> weakObj = new WeakReference<>(new Object()); // 弱引用

        // 清除强引用
        strongObj = null;

        // 执行垃圾回收
        System.gc();

        // 检查弱引用是否还有效
        if (weakObj.get() != null) {
            System.out.println("Weak reference still valid.");
        } else {
            System.out.println("Weak reference cleared.");
        }
    }
}
